package com.desay.pangoo.itmt.controller;

import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.desay.pangoo.common.lite.lang.web.CommonResponse;
import com.desay.pangoo.itmt.controller.vo.TechClassificationVo;
import com.desay.pangoo.itmt.enums.ExceptionCodeEnum;
import com.desay.pangoo.itmt.service.TechClassificationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 数据集
 *
 * @author uidq1163
 */
@RestController
@Api(tags = "ITMT Tech 分类管理")
@RequestMapping(value = "/techClassification")
@Validated
public class TechClassificationController {
	@Autowired
	private TechClassificationService techClassificationService;

	@PostMapping
	@ApiOperation(value = "添加工作组分类信息", notes = "添加工作组分类信息")
	public ResponseEntity<Object> create(@RequestBody TechClassificationVo vo) {
		return new ResponseEntity<>(CommonResponse.success(techClassificationService.create(vo)), HttpStatus.OK);
	}

	@DeleteMapping(value = "{id}")
	@ApiOperation(value = "删除", notes = "删除工作组分类信息")
	@ApiImplicitParam(name = "id", value = "工作组ID", required = true, dataTypeClass = Long.class, paramType = "path")
	public ResponseEntity<Object> delete(@PathVariable long id) {
		try {
            techClassificationService.delete(id);
        } catch (ConstraintViolationException | DataIntegrityViolationException e) {
            return new ResponseEntity<>(CommonResponse.failure(ExceptionCodeEnum.TECH_CLASSIFICATION_CHILDREN_EXIST,null), HttpStatus.OK);
        }
		return new ResponseEntity<>(CommonResponse.success(), HttpStatus.OK);
	}

	@PutMapping(value = "{id}")
	@ApiOperation(value = "更新工作组分类信息", notes = "更新工作组分类信息")
	public ResponseEntity<Object> update(@PathVariable(value = "id") Long id, @RequestBody TechClassificationVo vo) {
		techClassificationService.update(id, vo);
		return new ResponseEntity<>(CommonResponse.success(), HttpStatus.OK);
	}

	@GetMapping(value = "")
	@ApiOperation(value = "获取工作组分类列表", notes = "获取工作组分类信息列表")
	@ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", type = "int", value = "指定查询页数", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", type = "int", value = "每页数据数量", paramType = "query"),
			@ApiImplicitParam(name = "workGroupId", value = "数据集ID", type = "int", paramType = "query") })
	public ResponseEntity<Object> getAllList(@RequestParam(value = "workGroupId", required = false) Long workGroupId,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
		return new ResponseEntity<>(CommonResponse.success(techClassificationService.getList(pageSize, pageNo, workGroupId)),
				HttpStatus.OK);
	}
	
	@GetMapping(value = "all")
	@ApiOperation(value = "获取工作组分类列表", notes = "获取工作组分类信息列表")
	@ApiImplicitParams({ 
			@ApiImplicitParam(name = "workGroupId", value = "工作组ID", type = "int", paramType = "query") })
	public ResponseEntity<Object> getAllList(@RequestParam(value = "workGroupId", required = false) Long workGroupId) {
		return new ResponseEntity<>(CommonResponse.success(techClassificationService.getAllList(workGroupId)),
				HttpStatus.OK);
	}
}
